<?php
//defined( '_JEXEC' ) or die( 'Restricted access' );

/**************************************************************************************
* Class: Pager
* Methods:
* findStart
* findPages
* pageList
* nextPrev
* Redistribute as you see fit.
**************************************************************************************/
class Pager
{
/***********************************************************************************
* Ham int findStart (int limit)
* Tra ve dong bat dau cua trang duoc chon dua tren trang lay duoc va bien limit
***********************************************************************************/
	var $mau = "#ffffff";
	
	function findStart($limit)
	{
		if ((!isset($_GET['page'])) || ($_GET['page'] == "1"))
		{
			$start = 0;
			$_GET['page'] = 1;
		}
		else
		{
			$start = ($_GET['page']-1) * $limit;
		}
		
		return $start;
	}
/***********************************************************************************
* Ham int findPages (int count, int limit)
* Tra ve so luong trang can thiet dua tren tong so dong co trong table va limit
***********************************************************************************/
	function findPages($count, $limit)
	{
		if($limit==0 )
			$limit = 4;
		$pages = (($count % $limit) == 0) ? $count / $limit : floor($count / $limit) + 1;
		return $pages;
	}
/***********************************************************************************
* Ham: string pageList (int curpage, int pages)
* Tra ve danh sach trang theo dinh dang "Trang dau tien  < [cac trang] > Trang cuoi cung"
***********************************************************************************/
	function addLinktoBlock($string){
		
		return '<div  align="center"  class="DLPager">'.$string.'</div>';
	}
	function pageList($curpage, $pages)
	{
		$page_list = "<div style='float:left;padding-top:4px'><font color = '{$this->mau}'><b>"._PAGE."&nbsp;&nbsp;</b></font></div>";
		
		$currentGETLIST= Pager::getCurrentGETLIST();
	
		/* In trang dau tien va nhung link toi trang truoc neu can */
		if (($curpage != 1) && ($curpage))
		{
			$page_list .= $this->addLinktoBlock(" <a  href=\"".$_SERVER['PHP_SELF'].$currentGETLIST."page=1\" title=\""._PAGE_FIRST."\"><<</a> ");
		}
	
		if (($curpage-1) > 3)
		{
			$page_list .= $this->addLinktoBlock("<a  href=\"".$_SERVER['PHP_SELF'].$currentGETLIST."page=".($curpage-1)."\" title=\""._PAGE_BACK."\"><</a> ");
		}
	
		/* In ra danh sach cac trang va lam cho trang hien tai dam hon va mat link o chan*/
		for ($i=1; $i<=$pages; $i++)
		{
			if ($i == $curpage)
			{
				$page_list .="<div align='center' class='DLPager DLPagerActive'>".$i."</div>";
			}
			else
			{
				if(abs($curpage-$i)<4)
					$page_list .= $this->addLinktoBlock("<a  href=\"".$_SERVER['PHP_SELF'].$currentGETLIST."page=".$i."\" title=\""._PAGE." ".$i."\">".$i."</a>");
				else
					if(abs($curpage-$i)==4)
						$page_list .= $this->addLinktoBlock("...");
			}
			$page_list .= " ";
		}

		/* In link cua trang tiep theo va trang cuoi cung neu can*/
		if (($curpage+4) <= $pages)
		{
			$page_list .= $this->addLinktoBlock("<a  href=\"".$_SERVER['PHP_SELF'].$currentGETLIST."page=".($curpage+1)."\" title=\""._PAGE_NEXT."\">></a> ");
		}
		
		if (($curpage != $pages) && ($pages != 0))
		{
			$page_list .= $this->addLinktoBlock("<a href=\"".$_SERVER['PHP_SELF'].$currentGETLIST."page=".$pages."\" title=\""._PAGE_END."\">>></a> ");
		}
		$page_list .= "<div style='clear:both'></td>\n";

	return $page_list;
	}
	
	function getCurrentGETLIST()
	{
		$currentGETLIST="?";
		foreach( $_GET as $key=>$value )
		if ( $key != "page" ) $currentGETLIST .= $key."=".$value."&";
		return 	$currentGETLIST;
	}
/***********************************************************************************
* Ham string nextPrev (int curpage, int pages)
* Returns "Previous | Next" string for individual pagination (it's a word!)
***********************************************************************************/
	function nextPrev($curpage, $pages)
	{
		$currentGETLIST= Pager::getCurrentGETLIST();
			
		$next_prev = "";
		
		if (($curpage-1) <= 0)
		{
			$next_prev .= "Trang truoc";
		}
		else
		{
			$next_prev .= "<a href=\"".$_SERVER['PHP_SELF'].$currentGETLIST."page=".($curpage-1)."\">"._PAGE_BACK."</a>";
		}
		$next_prev .= " | ";
		
		if (($curpage+1) > $pages)
		{
			$next_prev .= "Trang sau";
		}
		else
		{
			$next_prev .= "<a href=\"".$_SERVER['PHP_SELF'].$currentGETLIST."page=".($curpage+1)."\">"._PAGE_NEXT."</a>";
		}
		return $next_prev;
	}
}
?>